
/**
 * 
 * @param status 1,2,3,4 判断名字
 */
export const getType = (status: number): string => {
    var result = ''
    switch (status) {
        case 1:
            result = '清醒'
            break
        case 2:
            result = '浅睡'
            break
        case 3:
            result = '深睡'
            break
        default:
            result = '离床'
            break
    }
    return result
}
/**
 * 判断状态
 * @param status 1,2,3,4 
 */
export const getStatus = (status: number): number => {
    let statusNum = status
    if (status == 4) {
        statusNum = 0
    }
    return statusNum
}

export const timestampToTime = (timestamp: string, useHour: boolean, useSecond: boolean): string => {
    let date = new Date(timestamp.replace(/-/g,"/"))
    let Y = date.getFullYear() + '/'
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/'
    let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let h = date.getHours() + ':'
    let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()

    if (!useHour) return Y + M + D
    else {
        if (useSecond) return Y + M + D + ' ' + h + m + ':' + s
        else return Y + M + D + ' ' + h + m
    }
}
/**
 * 判断时间
 * @param startTime 开始时间
 * @param endTime 结束时间
 */
export const getHour = (startTime: string, endTime: string): boolean => {
    let startTimes = startTime.substring(0, 10).split('/')
    let endTimes = endTime.substring(0, 10).split('/')
    startTime = startTimes[1] + '/' + startTimes[2] + '/' + startTimes[0] + ' ' + startTime.substring(10, 19)
    endTime = endTimes[1] + '/' + endTimes[2] + '/' + endTimes[0] + ' ' + endTime.substring(10, 19)
    let thisResult = (Date.parse(endTime) - Date.parse(startTime)) / 3600 / 1000
    if (thisResult < 0) {
        return true
    } else if (thisResult > 0) {
        return false
    } else if (thisResult == 0) {
        return false
    } else {
        return false
    }
}
/**
 * 日期减一天
 */
export const dateChange = (num = 1, date: any = false): string => {
    if (!date) {
        date = new Date() //没有传入值时,默认是当前日期
        date = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate()
    }
    date += ' 00:00:00' //设置为当天凌晨12点
    date = Date.parse(new Date(date)) / 1000 //转换为时间戳
    date += 86400 * num //修改后的时间戳
    var newDate = new Date(parseInt(date) * 1000) //转换为时间
    return newDate.getFullYear() + '/' + (newDate.getMonth() + 1) + '/' + newDate.getDate()
}

/**
 * 获得相对当前周AddWeekCount个周的起止日期 
 * @param AddWeekCount 为0代表当前周  为-1代表上一个周  为1代表下一个周以此类推
 */
export const getWeekStartAndEnd = (AddWeekCount: number): Array<string> => {
    //起止日期数组  
    var startStop = new Array();
    //一天的毫秒数  
    var millisecond = 1000 * 60 * 60 * 24;
    //获取当前时间  
    var currentDate = new Date();
    //相对于当前日期AddWeekCount个周的日期
    currentDate = new Date(currentDate.getTime() + (millisecond * 7 * AddWeekCount));
    //返回date是一周中的某一天
    var week = currentDate.getDay();
    //返回date是一个月中的某一天  
    var month = currentDate.getDate();
    //减去的天数  
    var minusDay = week != 0 ? week - 1 : 6;
    //获得当前周的第一天  
    var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay));
    //获得当前周的最后一天
    var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
    //添加至数组  
    startStop.push(getDateStr3(currentWeekFirstDay));
    startStop.push(getDateStr3(currentWeekLastDay));

    return startStop;
}

/**
 * 获得相对当月AddMonthCount个月的起止日期 
 * @param AddMonthCount 为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推
 */
export const getMonthStartAndEnd = (AddMonthCount: number): Array<string> => {
    //起止日期数组  
    var startStop = new Array();
    //获取当前时间  
    var currentDate = new Date();
    var month = currentDate.getMonth() + AddMonthCount;
    if (month < 0) {
        var n = parseInt((-month) / 12);
        month += n * 12;
        currentDate.setFullYear(currentDate.getFullYear() - n);
    }
    currentDate = new Date(currentDate.setMonth(month));
    //获得当前月份0-11  
    var currentMonth = currentDate.getMonth();
    //获得当前年份4位年  
    var currentYear = currentDate.getFullYear();
    //获得上一个月的第一天  
    var currentMonthFirstDay = new Date(currentYear, currentMonth, 1);
    //获得上一月的最后一天  
    var currentMonthLastDay = new Date(currentYear, currentMonth + 1, 0);
    //添加至数组  
    startStop.push(getDateStr3(currentMonthFirstDay));
    startStop.push(getDateStr3(currentMonthLastDay));
    //返回  
    return startStop;
}

//获取当前日期yy-mm-dd
//date 为时间对象
export const getDateStr3 = (date: Date):string => {
    var year = "";
    var month = "";
    var day = "";
    var now = date;
    year = "" + now.getFullYear();
    if ((now.getMonth() + 1) < 10) {
        month = "0" + (now.getMonth() + 1);
    } else {
        month = "" + (now.getMonth() + 1);
    }
    if ((now.getDate()) < 10) {
        day = "0" + (now.getDate());
    } else {
        day = "" + (now.getDate());
    }
    return year + "-" + month + "-" + day;
}

export const getDateStr4 = (date: Date):string => {
    var year = "";
    var month = "";
    var day = "";
    var now = date;
    year = "" + now.getFullYear();
    if ((now.getMonth() + 1) < 10) {
        month = "0" + (now.getMonth() + 1);
    } else {
        month = "" + (now.getMonth() + 1);
    }
    if ((now.getDate()) < 10) {
        day = "0" + (now.getDate()-1);
    } else {
        day = "" + (now.getDate()-1);
    }
    return year + "-" + month + "-" + day;
}